<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/11
 * Time: 17:28
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\api\controller\Cc;

class TimerTask extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    // 返回数据无需加密的方法,单个设置例如：['test','test2'],*表示全部,只对 $this->success()方法有效
    protected $noEncryption = '*';
    // 接收的数据无需解密的方法(只有调用Api类中的getpm()方法有效),单个设置例如：['test','test2'],*表示全部,只对 $this->getpm()方法有效
    protected $rNoEncryption = [];

    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub

        $host = $this->get_server_host();
        $server_ip = gethostbyname($host);
        $cip = $this->get_remote_ip();

        if($cip != $server_ip){
            die("ip:".$cip." Permission denied");
        }
//        setCc()
    }

    /**
     * 发放加速奖
     */
    public function actJ(){
        $bonus = new Bonus();
        $bonus->actJsj();
    }

    /**
     * 打开App
     */
    public function openA(){
        $is_auto = config('site.is_auto');
        if (!$is_auto){
            return 123;
        }

        $start = config('site.opentime')['开放时间'];
        $start = strtotime(date('Y-m-d '.$start, time()));

        $end = config('site.opentime')['关闭时间'];
        $end = strtotime(date('Y-m-d '.$end, time()));

        $now = time();
        if ($now<$start || $now>=$end){
            $value = 0;
        }else{
            $value = 1;
        }

        $dbvalue = db('config')->field('value')->where("name='app_status'")->find();
        if ($dbvalue != $value){
            $res = db('config')->where("name='app_status'")->update(['value'=>$value]);
            return $res;
        }else{
            return 'abc';
        }

    }

    //获取服务器域名
    private function get_server_host() {
        return isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
    }

    //获取访问的ip
    private function get_remote_ip() {
        return isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FORWARDED_FOR"]
            :(isset($_SERVER["HTTP_CLIENT_IP"])?$_SERVER["HTTP_CLIENT_IP"]
                :$_SERVER["REMOTE_ADDR"]);
    }

}